import { aesUtil, rsaUtil } from "./encrypt-util.js";
import store from "@/store";
import axios from "./axios";

var keyPair;
var serverPublicKey;
var offEncrypt = true; //是否关闭加密
//$(getServerPublicKey()); //若要加密取消注释

function getServerPublicKey() {
  //生成客户端rsa密钥对
  keyPair = rsaUtil.genKeyPair();
  //请求服务端rsa公钥
  axios.post(store.state.url + "index/getPublicKey").then(res => {
    console.log("服务端key:" + res.key);
    return (serverPublicKey = res.key);
  });
  // $.ajax({
  //     url:store.state.url+"index/getPublicKey",
  //     type:"post",
  //     success : function(data){
  //         console.log(data)
  //         console.log("服务端key:" + JSON.parse(data).key);
  //         serverPublicKey = JSON.parse(data).key;
  //     }
  // });
}

var axios_encrypt = function(url, data, isEncrypt = true) {
  //http请求发送+解密
  if (offEncrypt) {
    var params = data;
  } else {
    //aes对传输内容进行加密
    var genKey = aesUtil.genKey();
    console.log("客户端aeskey:" + genKey);
    // var plaintext = aesUtil.encrypt(data,genKey);
    //rsa服务端公钥对aes key加密
    if (!serverPublicKey) {
      getServerPublicKey();
      // return;
    }
    var ciphertext = rsaUtil.encrypt(genKey, serverPublicKey);
    console.log("aes key加密后" + ciphertext);
    //加密消息内容与rsa公钥传后台
    var objKey = {
      clientKey: ciphertext,
      clientPublicKey: keyPair.publicKey
    };
    if (isEncrypt) {
      console.log("需要加密");
      for (let i in data) {
        data[i] = aesUtil.encrypt(data[i], genKey);
      }
    }
    var params = Object.assign(objKey, data);
  }
  return new Promise((resolve, reject) => {
    axios
      .post(store.state.url + url, params)
      .then(res => {
        console.log(res, 65);
        if (res && res.resultkey) {
          //解析服务端aes key
          var serverKey = rsaUtil.decrypt(res.resultkey, keyPair.privateKey);
          //对传输内容解密
          // var data = aesUtil.decrypt(res.data,serverKey);
          let data = {};
          for (let i in res) {
            if (i != "resultkey") {
              data[i] = aesUtil.decrypt(res[i], serverKey);
            }
          }
          resolve(data);
        } else {
          resolve(res);
        }
      })
      .catch(err => {
        reject(err);
      });
  });
};

var ws_encrypt = function(data) {
  //ws加密
  if (!offEncrypt) {
    //aes对传输内容进行加密
    var genKey = aesUtil.genKey();
    console.log("客户端aeskey:" + genKey);
    // var plaintext = aesUtil.encrypt(data,genKey);
    //rsa服务端公钥对aes key加密
    if (!serverPublicKey) {
      getServerPublicKey();
      // return;
    }
    var ciphertext = rsaUtil.encrypt(genKey, serverPublicKey);
    console.log("aes key加密后" + ciphertext);
    for (let i in data.body) {
      data.body[i] = aesUtil.encrypt(data.body[i], genKey);
    }
    //加密消息内容与rsa公钥传后台
    var objKey = {
      clientKey: ciphertext,
      clientPublicKey: keyPair.publicKey
    };
    data.body = Object.assign(objKey, data.body);
    data.head.clientPublicKey = keyPair.publicKey;
  }
  return data;
};

var ws_decode = function(data) {
  //ws解密
  if (data && data.resultkey) {
    //解析服务端aes key
    var serverKey = rsaUtil.decrypt(data.resultkey, keyPair.privateKey);
    let data1 = {};
    for (let i in data) {
      if (i != "resultkey") {
        //对传输内容解密
        data1[i] = aesUtil.decrypt(data[i], serverKey);
      }
    }
    return data1;
  } else {
    return data;
  }
};

export { keyPair, ws_encrypt, axios_encrypt, ws_decode };
